//
//  XJYTimeViewController.m
//  ShopMall
//
//  Created by XJY on 17/1/17.
//  Copyright © 2017年 XinJinYing. All rights reserved.
//

#import "XJYTimeViewController.h"
#import <SDCycleScrollView.h>
#import "XJYTimeTwoBtnView.h"
#import "XJYGroupListModel.h"
#import "XJYSingleListModel.h"
#import "XJYSingleTabelView.h"
#import "XJYDetailsViewController.h"


@interface XJYTimeViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *mainScorll;   //最底层滑动视图
@property (nonatomic, strong) SDCycleScrollView *headImageView;  //轮播图
@property (nonatomic, strong) XJYTimeTwoBtnView *twoButtonView;  //中间切换列表的按钮
@property (nonatomic, strong) XJYSingleTabelView *singleTable;  //单品团购
@property (nonatomic, strong) XJYSingleTabelView *groupTable;   //品牌团购
@property (nonatomic, strong) NSArray *singleModelArray;        //存放新品数据模型的数组
@property (nonatomic, strong) NSArray *groupModelArray;         //存放团购数据模型的数组

@end

@implementation XJYTimeViewController

#pragma mark - 生命周期方法
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.edgesForExtendedLayout = 0;
    [self.view addSubview:self.mainScorll];
    [self.mainScorll addSubview:self.headImageView];
    
    [self.mainScorll addSubview:self.singleTable];
    //[self.mainScorll addSubview:self.groupTable];
    //为了防止向上滑动的时候，tableview的北京颜色盖住两个按钮的颜色，将按钮的view放在tableView的上面
    [self.mainScorll addSubview:self.twoButtonView];
    
    [self addSearchBtn];
    
    [self setupLayout];
}

- (void)addSearchBtn
{
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [searchBtn setImage:[UIImage imageNamed:@"限时特卖界面搜索按钮"] forState:UIControlStateNormal];
    searchBtn.frame = CGRectMake(0, 0, 35, 35);
    [searchBtn addTarget:self action:@selector(pushSearchViewController) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *navItem = [[UIBarButtonItem alloc] initWithCustomView:searchBtn];
    self.navigationItem.rightBarButtonItem = navItem;
}

#pragma mark - 懒加载
- (UIScrollView *)mainScorll
{
    if (!_mainScorll) {
        _mainScorll = [[UIScrollView alloc] init];
        _mainScorll.contentSize = CGSizeMake(0, 1980);
        _mainScorll.delegate = self;
    }
    return _mainScorll;
}

- (SDCycleScrollView *)headImageView
{
    if (!_headImageView) {
        _headImageView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 230) delegate:nil placeholderImage:[UIImage imageNamed:@"图标"]];
        _headImageView.backgroundColor = [UIColor orangeColor];
        _headImageView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        _headImageView.currentPageDotColor = [UIColor whiteColor];
    }
    return _headImageView;
}

- (XJYTimeTwoBtnView *)twoButtonView
{
    if (!_twoButtonView) {
        _twoButtonView = [[XJYTimeTwoBtnView alloc] initWithFrame:CGRectMake(0, 230, VIEW_WIDTH, 50)];
        _twoButtonView.backgroundColor = [UIColor whiteColor];
        [_twoButtonView.button2 addTarget:self action:@selector(changeTableFrame:) forControlEvents:UIControlEventTouchUpInside];
        [_twoButtonView.button1 addTarget:self action:@selector(changeTableFrame:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _twoButtonView;
}

- (XJYSingleTabelView *)singleTable
{
    if (!_singleTable) {
        _singleTable = [[XJYSingleTabelView alloc] initWithFrame:CGRectMake(0, 280, VIEW_WIDTH, 1700) style:UITableViewStylePlain];
        _singleTable.isSingle = YES;
        __weak typeof(self) weakSelf = self;
        _singleTable.goodsIDBlock = ^(NSString *goodID){
            XJYDetailsViewController *details = [[XJYDetailsViewController alloc] init];
            details.detailsGoodsId = goodID;
            [weakSelf.navigationController pushViewController:details animated:YES];
        };
    }
    return _singleTable;
}

- (XJYSingleTabelView *)groupTable
{
    if (!_groupTable) {
        _groupTable = [[XJYSingleTabelView alloc] initWithFrame:CGRectMake(VIEW_WIDTH, 280, VIEW_WIDTH, 1700) style:UITableViewStylePlain];
        _groupTable.isSingle = NO;
    }
    return _groupTable;
}

#pragma mark - 三个网络请求    轮播图请求   新品数据  团购数据
//请求轮播数据
//- (void)requestHeadImage
//{
//    [self getWithPath:url params:params success:^(id json) {
//        NSMutableArray *imageArray = [NSMutableArray arrayWithCapacity:0];
//        for (NSDictionary *dic in json) {
//            NSString *imageUrl = dic[@"ImgView"];
//            [imageArray addObject:imageUrl];
//        }
//        _headImageView.imageURLStringsGroup = imageArray;
//    } failure:^(NSError *error) {
//        XJYLog(@"error : %@",error);
//    }];
//}

#pragma mark - 布局
- (void)setupLayout
{
    __weak typeof(self) weakSelf = self;
    [_mainScorll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(weakSelf.view);
    }];
}

#pragma mark - scrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y > 230) {
        CGRect rect = _twoButtonView.frame;
        rect.origin.y = scrollView.contentOffset.y;
        _twoButtonView.frame = rect;
    } else {
        CGRect rect = _twoButtonView.frame;
        rect.origin.y = 230;
        _twoButtonView.frame = rect;
    }
}

#pragma mark - changeTabelFrame改变两个tableView
- (void)changeTableFrame:(UIButton *)button
{
    if (button == _twoButtonView.button1) {
        button.selected = YES;
        _twoButtonView.button2.selected = NO;
        [UIView animateWithDuration:0.3 animations:^{
            CGRect tableRect1 = _singleTable.frame;
            tableRect1.origin.x = 0;
            _singleTable.frame = tableRect1;
            
            CGRect tableRect2 = _groupTable.frame;
            tableRect2.origin.x = VIEW_WIDTH;
            _groupTable.frame = tableRect2;
            _mainScorll.contentSize = CGSizeMake(0, _singleModelArray.count * 170 + 280);
        }];
    } else {
        button.selected = YES;
        _twoButtonView.button1.selected = NO;
        [UIView animateWithDuration:0.3 animations:^{
            CGRect tableRect1 = _singleTable.frame;
            tableRect1.origin.x = -VIEW_WIDTH;
            _singleTable.frame = tableRect1;
            
            CGRect tableRect2 = _groupTable.frame;
            tableRect2.origin.x = 0;
            _groupTable.frame = tableRect2;
            _mainScorll.contentSize = CGSizeMake(0, _singleModelArray.count * 200 + 280);
        }];
    }
}



@end
